/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-07
 * Time: 19:28
 */
//https://leetcode.cn/problems/aMhZSa/submissions/650835203/
//给定一个链表的 头节点 head ，请判断其是否为回文链表。
//如果一个链表是回文，那么链表节点序列从前往后看和从后往前看是相同的。
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Test {
    public static boolean isPalindrome(ListNode head) {
        ListNode p1;
        ListNode p2;
        p1=head;
        p2=head;
        while(p2!=null&&p2.next!=null){
            p1=p1.next;
            p2=p2.next.next;
        }
        p2=p1.next;
        ListNode p3;
        while(p2!=null){
            p3=p2.next;
            p2.next=p1;
            p1=p2;
            p2=p3;
        }
        p2=head;
        while(p2!=p1){
            if(p2.val!=p1.val){
                return false;
            }
            p2=p2.next;
            if(p2==p1){
                break;
            }
            p1=p1.next;
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode p1=new ListNode(1);
        ListNode p2=new ListNode(2);
        ListNode p3=new ListNode(2);
        ListNode p4=new ListNode(1);
        p1.next=p2;
        p2.next=p3;
        p3.next=p4;
        boolean bool=isPalindrome(p1);
        System.out.println(bool);
        ListNode A1=new ListNode(1);
        ListNode A2=new ListNode(2);
        A1.next=A2;
        System.out.println(isPalindrome(A1));
    }

}
